package com.cnpc.ds.service.impl.user;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.cnpc.eisc.base.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Component
@Slf4j
public class KyUserUtil {

    /**
     * http://11.71.3.110:30010  旧开发
     * http://11.71.250.53:30010 新开发
     * http://api.iosp.xjyt.petrochina  正式
     */
    static String prodApiUrl = "http://okdring.klmy.eisc.cnpc";

    public static User getUser(String token) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String url = prodApiUrl + "/api/base-auth-service/curuser";
        String body = HttpUtil.createGet(url).header("Authorization",token).execute().body();
        Map<String, Object> data = JSONObject.parseObject(body, Map.class);
        Map<String, Object> curUserInfo = (Map<String, Object>) data.get("data");
        String userId = curUserInfo.get("userId").toString();
        log.info("接口: {}, 获取到用户信息 --> id: {}, name: {}", request.getRequestURI(), userId, curUserInfo.get("realName"));
        User userVo = new User();
        userVo.setUserId(userId);
        return userVo;
    }


}
